package moon.core.classdef;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import moon.core.Model;
import moon.core.Name;
import moon.core.NamedObject;
import moon.traverse.Visitor;

/**
 * Name space.
 * 
 * @author <a href="mailto:rmartico@ubu.es">Ra&uacute;l Marticorena</a>
 * @since JavaMoon-2.0
 */
@SuppressWarnings("serial")
public abstract class NameSpace extends NamedObject {

	/**
	 * Classes contained in this namespace.
	 */
	private Map<Name,ClassDef> classes;
	
	/**
	 * Constructor.
	 * 
	 * @param name name
	 */
	public NameSpace(Name name){
		super(name);
		classes = new HashMap<Name,ClassDef>();
	}
	
	/**
	 * Gets the unique name.
	 */
	@Override
	public Name getUniqueName(){
		return getName(); 
	}
	
	/**
	 * Adds a class to the namespace.
	 * 
	 * @param classDef class
	 */
	public void add(ClassDef classDef){
		if (!classes.containsKey(classDef.getUniqueName())){
			classes.put(classDef.getUniqueName(),classDef);
			classDef.setNameSpace(this);
		}
	}
	
	/**
	 * Removes a class to the namespace.
	 * 
	 * @param classDef class
	 */
	public void remove(ClassDef classDef){
		if (!classes.containsKey(classDef.getUniqueName())){
			classes.remove(classDef.getUniqueName());
			classDef.setNameSpace(null);
		}
	}
	
	/**
	 * Gets the classes of this namespace.
	 * 
	 * @return classes of this namespace
	 */
	public List<ClassDef> getClassDef(){
		return new ArrayList<ClassDef>(classes.values());
	}	
	
    /**
     * Method from Pattern Design Visitor.
     * 
     * Note: by RMS.
     * 
     * @param visitor visitor
     */
    public void accept(Visitor visitor){
    	// Hook method
    	visitor.visit(this);
    }  

    /**
     * Gets the model.
     * 
     * @return model
     */
    public abstract Model getModel();
} // NameSpace
